package com.example.coding_problem.Fragment;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.ashokvarma.bottomnavigation.utils.Utils;
import com.example.coding_problem.R;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Code2 extends Fragment{
    private GridView gridView;
    private List<String> Name =new ArrayList<>();
    private Uri[] Mp4={ Uri.parse( "/sdcard/Mission.mp4"),  Uri.parse( "/sdcard/Mission.mp4"),  Uri.parse( "/sdcard/Mission.mp4"),  Uri.parse( "/sdcard/Mission.mp4")};
    public Code2() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_code2, container, false);
        Name.add("视频文件一");
        Name.add("视频文件一");
        Name.add("视频文件一");
        Name.add("视频文件一");
        initView(view);
        return view;
    }
    private class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText(getContext(), "播放完成了", Toast.LENGTH_SHORT).show();
        }
    }
    private void initView(View view){
        gridView=(GridView)view.findViewById(R.id.gridView);
        gridView.setAdapter(new gAdapter());

    }
    private class gAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return Name.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=View.inflate(getContext(),R.layout.play_video,null);
            final VideoView videoView = view1.findViewById(R.id.video_view);
            TextView textView = view1.findViewById(R.id.tv_text);
            textView.setText(Name.get(i));
            //设置视频控制器
            videoView.setMediaController(new MediaController(getContext()));
            //设置视频路径
            videoView.setVideoURI(Mp4[i]);
            //播放完成回调
            videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    videoView.start();
                }
            });
            return view1;
        }
    }
}
